package com.bjtime.designer.widget

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.media.Image
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bjtime.designer.Jutils
import com.bjtime.designer.R
import com.bjtime.designer.attributes.*
import com.bjtime.designer.utils
import java.io.ByteArrayOutputStream
import java.io.File


class ImageView2 : BaseLabelView, View.OnTouchListener {

    var inflater : LayoutInflater
    var mLayoutView : View
    var mBorderView : View
    var mWBtn       : View
    var mHBtn       : View
    var des_image     : Bitmap? = null
    var mScale      : Int = 0 // 缩放方式, 拉伸,居中.em.....
    var mImageView  : ImageView? = null
    val TagViewName = "ImageView2"
    var des_scale   : Int = 0

    @JvmOverloads
    constructor(context: Context,byte:ByteArray, attrs: AttributeSet ? = null, defStyleAttr: Int = 0) : super(context, attrs, defStyleAttr) {
        des_viewtype = ViewAttr.VIEW_TYPE.IMAGE
        inflater = LayoutInflater.from(this.context)
        mLayoutView = inflater.inflate(R.layout.layout_designer_label_imageview2, null)
        this.addView(mLayoutView)
        mImageView = mLayoutView.findViewById<ImageView>(R.id.imageview2_label_imageview)

        mBorderView = mLayoutView.findViewById<View>(R.id.imageview2_label_broderview)
        mWBtn = mLayoutView.findViewById(R.id.imageview2_label_right_btn)
        mHBtn = mLayoutView.findViewById(R.id.imageview2_label_bottom_btn)

//        val a = context.obtainStyledAttributes(attrs, R.styleable.ImageView2)
//        val rid = a.getResourceId(R.styleable.ImageView2_image, R.drawable.bz)

        val a = context.obtainStyledAttributes(attrs, R.styleable.ImageView2)
        des_image = BitmapFactory.decodeByteArray (byte, 0, byte.size)

        a.recycle()
        mImageView!!.setImageBitmap(des_image)

//        des_image = BitmapFactory.decodeResource(resources, rid)
//        a.recycle()

        mHBtn.setOnTouchListener(this)
        mWBtn.setOnTouchListener(this)
        setSelect (false)
    }

    private var xDelta: Float = 0f
    private var yDelta: Float = 0f

    override fun onTouch(v: View, event: MotionEvent): Boolean {
        val x = event.x
        val y = event.y

        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                xDelta = x - super.getWidth()
                yDelta = y - super.getHeight()
                super.requestDisallowInterceptTouchEvent(true)
            }
            MotionEvent.ACTION_MOVE -> {
                xDelta = width.toFloat()
                yDelta = height.toFloat()

                val params = super.getLayoutParams()
                val xDistance = x + xDelta
                val yDistance = y + yDelta
                if (v.id == mWBtn.id) {
                    params.width = xDistance.toInt()
                } else {
                    params.height = yDistance.toInt()
                }
                super.setLayoutParams(params)
            }
            MotionEvent.ACTION_UP -> super.requestDisallowInterceptTouchEvent(false)
        }
        return true
    }

    override fun setSelect(select: Boolean) {
        super.setSelect(select)
        if (!select) {
            mBorderView.setBackgroundResource(0)
            mWBtn.visibility = View.GONE
            mHBtn.visibility = View.GONE
        } else {
            if (!des_lock) {
                mBorderView.setBackgroundResource(R.drawable.shape_designer_label_border)
                mWBtn.visibility = View.VISIBLE
                mHBtn.visibility = View.VISIBLE
            } else {
                mBorderView.setBackgroundResource(R.drawable.shape_designer_label_border_locked)
                mWBtn.visibility = View.GONE
                mHBtn.visibility = View.GONE
            }
        }
    }

    override fun getAttr (): ImageAttr {
        var attr  = ImageAttr ();
        attr.des_id = des_id
        attr.des_left = des_left
        attr.des_top = des_top
        attr.des_width = des_width
        attr.des_height = des_height
        attr.des_lock = des_lock
        attr.des_ptype = des_ptype
        attr.des_rotate = des_rotate
        attr.des_image  = utils.Bitmap2ByteArray (des_image!!)
        attr.des_scale  = mScale
        attr.des_viewtype = ViewAttr.VIEW_TYPE.IMAGE

        attr.prt_bottom = prt_bottom
        attr.prt_right  = prt_right
        attr.prt_top    = prt_top
        attr.prt_left   = prt_left

        return attr
    }


    override fun setAttr (attr: BaseAttr?) {
        attr as ImageAttr
        des_id = attr.des_id
        des_left = attr.des_left
        des_top = attr.des_top
        des_width = attr.des_width
        des_height = attr.des_height
        des_lock = attr.des_lock
        des_ptype = attr.des_ptype
        des_rotate = attr.des_rotate
        des_reversed = attr.des_reversed
        des_viewtype = attr.des_viewtype
        des_scale = attr.des_scale
        setImage(attr.des_image!!)
        setType (des_scale)
    }


    fun setImage (bs:ByteArray) {
        des_image = BitmapFactory.decodeByteArray (bs, 0, bs.size)
        mImageView!!.setImageBitmap(des_image)
    }

    fun setType (t:Int) {
        des_scale = t
        if (t == 1) {
            mImageView!!.scaleType = ImageView.ScaleType.FIT_XY
        } else if (t == 0) {
            mImageView!!.scaleType = ImageView.ScaleType.FIT_CENTER
        }
    }

    fun getType () : Int {
        return des_scale
    }

    override fun onMoved(spac: Int) {
        super.onMoved(spac)
        prt_left = (des_left + utils.dp2px(context, 11f)) / spac
        prt_top = (des_top + utils.dp2px(context, 11f)) / spac
        prt_right = mImageView!!.width / spac //(des_width - utils.dp2px(context, 12f)) / spac
        prt_bottom =mImageView!!.height / spac // (des_height - utils.dp2px(context, 12f)) / spac
    }

    override fun toString (): String {
        val s = "ViewName    : " + TagViewName  + "\n" +
                "view_id     : " + des_id       + "\n" +
                "view_left   : " + des_left     + "\n" +
                "view_top    : " + des_top      + "\n" +
                "view_width  : " + des_width    + "\n" +
                "view_height : " + des_height   + "\n" +
                "view_islock : " + des_lock     + "\n" +
                "view_select : " + des_select   + "\n" +
                "view_ptype  : " + des_ptype    + "\n" +
                "view_rotate : " + des_rotate   + "\n"
        return s
    }

}